// 云函数入口文件
const cloud = require('wx-server-sdk')
var request = require('request')

cloud.init({
    env:'tfjkp-0ge80vu9ea710148'
})

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()

    if(event.type == "addCount"){

        try{
          const result = await db.runTransaction(async transaction => {
              const shopInfo = await transaction.collection('maOnline').doc("0a4ec1f9628ef41a06cfecf818df40d5").get();
              if(shopInfo.data){
                  const updateShopInfo = await transaction.collection('maOnline').doc("0a4ec1f9628ef41a06cfecf818df40d5").update({
                      data:{
                        count:  _.inc(1)
                      }
                  })
              }else {
                  await transaction.rollback(-100)
              }    
          })
          }catch(e){
              console.error('transacation error',e)
          }
     
      }else if(event.type == "decCount"){
        try{
          const result = await db.runTransaction(async transaction => {
              const shopInfo = await transaction.collection('maOnline').doc("0a4ec1f9628ef41a06cfecf818df40d5").get();
              if(shopInfo.data){
                  if(shopInfo.data.count > 0){
                    const updateShopInfo = await transaction.collection('maOnline').doc("0a4ec1f9628ef41a06cfecf818df40d5").update({
                        data:{
                          count:  _.inc(-1)
                        }
                    })
                  }

              }else {
                  await transaction.rollback(-100)
              }    
          })
          }catch(e){
              console.error('transacation error',e)
          }
      }else if(event.type == "addImsUserId"){
        try{
          const result = await db.runTransaction(async transaction => {
              const shopInfo = await transaction.collection('maOnline').doc("0a4ec1f9628f339706dea414514c4887").get();
              
              if(shopInfo.data){
               
                const updateShopInfo = await transaction.collection('maOnline').doc("0a4ec1f9628f339706dea414514c4887").update({
                    data:{
                        'stuId':  _.push(event.stuId)
                    }
                })
                  
                
              }else {
                  await transaction.rollback(-100)
              }    
          })
          }catch(e){
              console.error('transacation error',e)
          }
      }else if(event.type == "decImsUserId"){
        try{
          const result = await db.runTransaction(async transaction => {
              const shopInfo = await transaction.collection('maOnline').doc("0a4ec1f9628f339706dea414514c4887").get();
              
              if(shopInfo.data.stuId.length > 0){
                const updateShopInfo = await transaction.collection('maOnline').doc("0a4ec1f9628f339706dea414514c4887").update({
                    data:{
                        'stuId':  _.pull(event.stuId)
                    }
                })
                  
                
              }else {
                  await transaction.rollback(-100)
              }    
          })
          }catch(e){
              console.error('transacation error',e)
          }
      }else if(event.type == "updateImsStas"){
        return new Promise((resolve, reject) => {
            var getOrderInfo = {
                "shId": event.body.shId
            }
            console.log(getOrderInfo)
            request({
                url: event.url,
                method: "POST",
                json: true,
                headers: {
                  "content-type": "application/json",
              
                },
                body:getOrderInfo
              }, function (error, response, body) {  
                console.log(body)         
                if (!error && response.statusCode == 200) {
                  try {
                    resolve(body)
                  } catch (e) {
                    reject()
                  }
                }
              })
            })
      }else if(event.type == "checkImsStas"){
        return new Promise((resolve, reject) => {
            var getOrderInfo = {
                "stuId":event.body.stuId,
                "shopId":event.body.orderId
            }
            console.log(getOrderInfo)
            request({
                url: event.url,
                method: "POST",
                json: true,
                headers: {
                  "content-type": "application/json",
              
                },
                body:getOrderInfo
              }, function (error, response, body) {  
                console.log(body)         
                if (!error && response.statusCode == 200) {
                  try {
                    resolve(body)
                  } catch (e) {
                    reject()
                  }
                }
              })
            })
      }
}